package commEntity

import (
	"encoding/json"
)

type IotDeviceModelEntity struct {
	BaseEntity *BaseEntity // 基本信息

	ModelName string // 设备厂商名称
	ModelType string // 设备类型名
	Provider  string // 设备名称

	ServiceParam map[string]interface{} // 配置参数
	ModelSchema  map[string]interface{} // 扩展参数
}

type iotDeviceModelPo struct {
	Id           int64                  `json:"id" db:"id"`                      // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`     // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`     // 更新时间
	ModelName    string                 `json:"modelName" db:"model_name"`       // 设备厂商名称
	ModelType    string                 `json:"modelType" db:"model_type"`       // 设备类型名
	Provider     string                 `json:"provider" db:"provider"`          // 设备名称
	ServiceParam map[string]interface{} `json:"serviceParam" db:"service_param"` // 配置参数
	ModelSchema  map[string]interface{} `json:"modelSchema" db:"model_schema"`   // 扩展参数
}

func (e *IotDeviceModelEntity) Type() string {
	return "IotDeviceModelEntity"
}

func (e *IotDeviceModelEntity) TableName() string {
	return "tb_iot_device_model"
}

func (e *IotDeviceModelEntity) GetBaseEntity() *BaseEntity {
	return e.BaseEntity
}

func (e *IotDeviceModelEntity) Instance() *IotDeviceModelEntity {
	return &IotDeviceModelEntity{
		BaseEntity:   new(BaseEntity),
		ServiceParam: make(map[string]interface{}),
		ModelSchema:  make(map[string]interface{}),
	}
}

func (e *IotDeviceModelEntity) NewEntity() IEntity {
	return e.Instance()
}

func (e *IotDeviceModelEntity) NewPo() interface{} {
	return &iotDeviceModelPo{
		ServiceParam: make(map[string]interface{}),
		ModelSchema:  make(map[string]interface{}),
	}
}

func (e *IotDeviceModelEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return MakeServiceKey(p)
}

func (e *IotDeviceModelEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.ModelName)
	return p
}

func (e *IotDeviceModelEntity) BuildByJson(jsn *string) error {
	var po iotDeviceModelPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *IotDeviceModelEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *IotDeviceModelEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*iotDeviceModelPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.ServiceParam = po.ServiceParam
	ptr.ModelSchema = po.ModelSchema
	ptr.ModelName = po.ModelName
	ptr.ModelType = po.ModelType
	ptr.Provider = po.Provider
}

func (e *IotDeviceModelEntity) BuildToPo() interface{} {
	ptr := e
	po := iotDeviceModelPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.ServiceParam = ptr.ServiceParam
	po.ModelSchema = ptr.ModelSchema
	po.ModelName = ptr.ModelName
	po.ModelType = ptr.ModelType
	po.Provider = ptr.Provider

	return &po
}
